<template>
	<view>
		<utop :istype="1"></utop>
		<view class="" v-if="ishide">
			<scroll-view scroll-y="true" style="height: 100vh;" :refresher-enabled='true' :refresher-threshold='30'
				refresher-background='#F7F8FA' @refresherrefresh='refresherrefresh' :refresher-triggered='isdown'>

				<view class="content">
					<view class="flex_bet items">
						<view class="textLeft">规格型号</view>
						<view class="rightbt">{{orderinfo.xinghao_name?orderinfo.xinghao_name:""}}
							{{orderinfo.color_name?orderinfo.color_name:''}}
						</view>
					</view>
					<view class="flex_bet item_flex">
						<view class="itemss">
							<view class="textLeft">板子大小</view>
							<view class="imgflex" @click="selectBanz">
								<view class="imgflex2" v-if="big">
									{{big}}
								</view>
								<view class="" v-else>
									请选择<image src="../../static/icon4.png" mode=""></image>
								</view>
							</view>
						</view>
						<view class="itemss">
							<view class="textLeft">理论片数</view>
							<view class="imgflex1" v-if="num">
								{{num}} <span>片</span>
							</view>
							<view class="imgflex" v-else>
								请先选择板子大小
							</view>
						</view>
					</view>
					<view class="flex_bet items">
						<view class="textLeft">制作片数</view>
						<view class="rightinput">
							<input confirm-type='done' placeholder-class="ustyle" v-model="nownum" type="number"
								placeholder="请输入" />
						</view>
					</view>
					<view class="btnlist">
						<view class="btn b1" @click="handleBf">部分完成</view>
						<view class="btn b2" @click="handleAll">全部完成</view>
					</view>
				</view>
				<view class="footer">
					<view class="utitle" v-if="!nolist">总计报工数量：{{allNum}}</view>
					<scroll-view class="clist" scroll-y="true" style="height: 40vh;" :show-scrollbar='false'
						@scrolltolower='onbottom'>
						<view class="" v-for="(item,index) in ulist" :key="index" style="margin-bottom: 20rpx;">
							<view class="ut flex_bet">
								<view class="t1">报工数量</view>
								<view class="t2">{{item.num}}</view>
							</view>
							<view class="utexts">
								<view class="">报工人：{{item.user_name}}</view>
								<view class="">报工时间：{{item.updatetime | formatDate}}</view>
								<view class="">报工数量：{{item.num}}</view>
								<view class="">报工单位：{{item.unit}}</view>
							</view>
						</view>
						<uni-load-more v-if="!ulist.length <= 0" :status="statusshow"></uni-load-more>
						<view class="" v-if="nolist">
							<view class="textLeft">报工记录</view>
							<view class="imgs">
								<image src="../../static/k.png" mode=""></image>
								<view class="ts">
									暂无报工明细
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</scroll-view>
		</view>
		<uni-popup ref="popup" type="bottom" :safe-area="false">
			<view class="popup">
				<view class="tit">选择板子大小</view>
				<uni-icons @click="close(1)" color="#979797" size="24" class="icon" type="closeempty"></uni-icons>
				<view class="pflex">
					<view :class="index==active?'itempactive':'itemp'" v-for="(item,index) in list" :key="index"
						@click="qie(item,index)">
						{{item.size_num}}
					</view>
				</view>
			</view>
		</uni-popup>

		<uni-popup ref="popups" type="center">
			<view class="popup1">
				<!-- 第一次报工 -->
				<view class="">
					<uni-icons @click="close(2)" color="#979797" size="24" class="icon" type="closeempty"></uni-icons>
					<view class="img">
						<image v-if="isall == 1 || isall == 3" src="../../static/info.png" mode=""></image>
						<image v-if="isall == 2" src="../../static/err.png" mode=""></image>
						<image v-if="isall == 4" src="../../static/err.png" mode=""></image>
					</view>
					<view class="texts" v-if="isall == 3">
						<view class="" v-if="ulist.length ==0 ">
							此次部分完成报工数量：<span style="color: #196AEB;">{{nownum}}</span>
						</view>

						<view class="" v-else>
							<view class="">
								已经报工数量：<span>{{allNum}}</span>
							</view>
							<view class="">
								此次报工数量：<span style="color: #196AEB;">{{nownum}}</span>
							</view>
							<view class="">
								总计报工数量：<span>{{Number(allNum)+Number(nownum)}}</span>
							</view>
						</view>
					</view>
					<view class="texts" v-else>
						<view class="" v-if="ulist.length !=0 ">
							已经报工数量：<span>{{allNum}}</span>
						</view>
						<view class="">
							此次报工数量：<span style="color: #196AEB;">{{nownum}}</span>
						</view>
						<view class="">
							总计报工数量：<span>{{Number(allNum)+Number(nownum)}}</span>
						</view>
					</view>

					<view class="context" v-if="isall == 1">
						是否确认<span style="color: #196AEB;">全部报工</span>已完成？
					</view>
					<view class="context" v-if="isall == 2">
						您提交<span style="color: #196AEB;">全部报工</span>的片数小于理论片数 需提交至管理员审核，是否确认提交?
					</view>
					<view class="context" v-if="isall == 4">
						您提交<span style="color: #196AEB;">全部报工</span>的片数大于理论片数 需提交至管理员审核，是否确认提交?
					</view>
					<view class="context" v-if="isall == 3">
						是否确认<span style="color: #196AEB;">部分报工</span>已完成？
					</view>
					<view class="bnts">
						<view class="btn b1" @click="close(2)">
							取消
						</view>
						<view class="btn b2" @click="handleTj">
							确认提交
						</view>
					</view>
				</view>
				<!-- 多次报工 -->
			</view>
		</uni-popup>
		<uni-popup ref="popupYes" type="center" :is-mask-click="false">
			<view class="yes">
				<view class="img">
					<image src="../../static/yes.png" mode=""></image>
				</view>
				<view class="ts">
					您已提交此订单全部完成，不支持再操作
				</view>
				<view class="" style="display: flex;align-items: center;">
					<view class="btn" @click="goi">
						首页
					</view>
					<view class="btn" @click="goindex">
						扫描新订单
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getOrderInfo,
		getOrderboard,
		getOrderLogList,
		setxisuWork
	} from '@/common/http.api.js'
	export default {
		data() {
			return {
				ishide: false,
				isdown: true,
				list: [],
				active: 0,
				big: '',
				num: '',
				nownum: '',
				isall: 1,
				ulist: [],
				orderinfo: {},


				params: {
					order_id: 6,
					page: 1,
					limit: 10
				},
				tjparams: {
					order_id: 6,
					board_id: '',
					num: '', //数量
					finish_type: '', //类型
				},
				statusshow: 'loading',
				nolist: true,
				isnoopen: false,
				order_id: 6,
				allNum: 0,
				userInfo: uni.getStorageSync('userinfo')
			};
		},
		onLoad(option) {
			console.log(option);
			this.order_id = option.id
			this.params.order_id = option.id
			this.tjparams.order_id = option.id
			this.getOrderInfoFun()
			this.getOrderlist()
		},
		methods: {
			goi(){
				uni.redirectTo({
					url:'/pages/index/index'
				})
			},
			refresherrefresh(e) {
				this.params.page = 1
				this.isdown = true
				this.ulist = []
				this.getOrderInfoFun()
				this.getOrderlist()
				setTimeout(() => {
					this.isdown = false
					uni.showToast({
						icon: 'none',
						title: '数据已更新'
					})
				}, 1000)
			},
			getOrderInfoFun() {
				getOrderInfo({
					order_id: this.order_id
				}).then(res => {
					this.orderinfo = res.data
					this.allNum = res.data.xisu_finish_num
					if (res.data.status >= 3) {
						this.ishide = false
						this.$refs.popupYes.open()
					} else {
						this.ishide = true
					}
					if (res.data.banzi_name) {
						this.big = res.data.banzi_name
						this.num = res.data.lilun_num
						this.isnoopen = true
					}
				})
			},
			getOrderlist() {
				this.statusshow = 'loading'
				getOrderLogList(this.params).then(res => {
					if (res.code == 1) {
						this.ulist.push(...res.data.list)
						this.statusshow = 'no-more'
						let total = Math.ceil(res.data.total_page / this.params.limit);
						this.nolist = total == 0
					}
				})
			},
			onbottom() {
				this.params.page++
				this.getOrderlist()
			},
			goindex() {
				let that = this
				uni.scanCode({
					barCodeInput: true,
					onlyFromCamera: true,
					success: function(res) {
						if (res.errMsg == "scanCode:ok") {
							let id = res.path.substring(res.path.lastIndexOf('?') + 1, res.path.length)
								.substring(9)
							setTimeout(() => {
								that.handleSm(id)
								that.$refs.popupYes.close()
							}, 500)
						}
					},
				})
			},
			handleSm(id) {
				if (this.userInfo.type == 1) {
					uni.redirectTo({
						url: `/pages/index/xspage?id=${id}`
					})
				}
				if (this.userInfo.type == 2) {
					uni.redirectTo({
						url: `/pages/index/ympage?id=${id}`
					})
				}
				if (this.userInfo.type == 3) {
					uni.redirectTo({
						url: `/pages/index/xppage?id=${id}`
					})
				}
				if (this.userInfo.type == 4) {
					uni.redirectTo({
						url: `/pages/index/xzpage?id=${id}`
					})
				}

			},
			selectBanz() {
				getOrderInfo({
					order_id: this.order_id
				}).then(res => {
					if (res.data.banzi_name) {
						this.orderinfo = res.data
						this.allNum = res.data.xisu_finish_num
						this.big = res.data.banzi_name
						this.num = res.data.lilun_num
						this.params.page = 1
						this.ulist = []
						this.getOrderlist()
						return
					} else {
						getOrderboard().then(res => {
							this.list = res.data
							this.$refs.popup.open()
						})
					}
				})

			},
			qie(item, index) {
				this.big = item.size_num
				this.active = index
				this.$refs.popup.close()
				this.num = Math.round(this.orderinfo.bao_num / item.bao_num)
				this.tjparams.board_id = item.id
			},
			close(type) {
				if (type == 1) {
					this.$refs.popup.close()
				} else {
					this.$refs.popups.close()
				}
			},
			handleBf() {
				if (this.big == '') {
					uni.showToast({
						icon: 'none',
						title: '请选择板子大小'
					})
					return
				}
				if (this.nownum == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入制作片数'
					})
					return
				}
				this.tjparams.finish_type = 1
				this.isall = 3
				this.$refs.popups.open()
			},
			handleAll() {
				if (this.big == '') {
					uni.showToast({
						icon: 'none',
						title: '请选择板子大小'
					})
					return
				}
				if (this.nownum == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入制作片数'
					})
					return
				}
				this.tjparams.finish_type = 2
				// (Number(this.nownum) + this.allNum) 总包工数量
				// 总包工数量< this.num 理论片数
				// 总包工数量> this.num 理论片数
				let allnum = (Number(this.nownum) + this.allNum)
				//this.isall = 1 不报警
				if (allnum > (this.num+15)) {
					this.isall = 4
					this.$refs.popups.open()
				} else if (allnum < this.num){
					this.isall = 2
					this.$refs.popups.open()
				}else{
					this.isall = 1
					this.$refs.popups.open()
				}
			},
			//点击提交按钮
			handleTj() {

				// if ((this.nownum * 1 + this.allNum) >= this.num) {
				// 	this.$refs.popupYes.open()
				// } else {
				if (this.ulist.length > 0) {
					this.tjparams.board_id = ''
				}
				this.tjparams.num = this.nownum
				setxisuWork(this.tjparams).then(res => {
					if (res.code == 1) {
						this.params.page = 1
						getOrderLogList(this.params).then(res => {
							if (res.code == 1) {
								this.ulist = res.data.list
								this.statusshow = 'no-more'
								let total = Math.ceil(res.data.total_page / this.params.limit);
								this.nolist = total == 0
								this.nownum = ''
								this.$refs.popups.close()
								this.getOrderInfoFun()
								if (this.tjparams.finish_type == 2) {
									this.$refs.popupYes.open()
								}
							}
						})

					}
				})

			}
		}
	}
</script>

<style lang="scss">
	.ustyle {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 30rpx;
		color: #979797;
	}

	page {
		background-color: #fff;
	}

	.content {
		background-color: #F7F8FA;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.items {
		width: 710rpx;
		height: 120rpx;
		background: #FFFFFF;
		border-radius: 22rpx;
		padding: 34rpx 30rpx 34rpx 44rpx;
		box-sizing: border-box;
		margin: 0rpx auto;

		.rightbt {
			height: 60rpx;
			background: #F2F8FF;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			border: 1rpx solid #196AEB;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #196AEB;
			line-height: 60rpx;
			text-align: center;
			padding: 0 10rpx;
		}

		.rightinput {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 30rpx;
			flex: 1;

			input {
				font-family: DIN, DIN;
				font-weight: bold;
				font-size: 40rpx;
				color: #196AEB;
				text-align: right;
			}
		}
	}

	.item_flex {
		width: 710rpx;
		margin: 20rpx auto;

		.itemss {
			width: 350rpx;
			height: 200rpx;
			background: #FFFFFF;
			border-radius: 22rpx;
			padding: 34rpx 44rpx;
			box-sizing: border-box;

			.imgflex {
				margin-top: 28rpx;
				display: flex;
				align-items: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #979797;

				image {
					width: 22rpx;
					height: 26rpx;
					margin-left: 20rpx;
				}
			}

			.imgflex1 {
				margin-top: 28rpx;
				font-family: DIN, DIN;
				font-weight: bold;
				font-size: 40rpx;
				color: #000000;

				span {
					font-size: 30rpx;
					margin-left: 6rpx;
				}
			}

			.imgflex2 {
				font-family: DIN, DIN;
				font-weight: bold;
				font-size: 40rpx;
				color: #196AEB;
			}
		}
	}

	.btnlist {
		width: 710rpx;
		display: flex;
		justify-content: space-between;
		margin: 20rpx auto 0rpx;

		.btn {
			width: 350rpx;
			height: 90rpx;
			border-radius: 50rpx 50rpx 50rpx 50rpx;
			text-align: center;
			line-height: 90rpx;
		}

		.b1 {
			border: 1rpx solid #196AEB;
			background-color: #FFFFFF;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #196AEB;
		}

		.b2 {
			background: #196AEB;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #ffffff;
		}
	}

	.footer {
		width: 100%;
		background-color: #FFFFFF;
		padding: 34rpx 32rpx;
		box-sizing: border-box;

		.imgs {
			text-align: center;

			image {
				display: block;
				margin: 80rpx auto 30rpx;
				width: 482rpx;
				height: 340rpx;
			}

			.ts {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #979797;
			}
		}

		.utitle {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 40rpx;
			color: #000000;
			line-height: 47rpx;
			width: 100%;
			margin-bottom: 30rpx;
		}

		.clist {
			.ut {
				border-top: 1px solid #D1D1D6;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 36rpx;
				padding-top: 32rpx;

				.t1 {
					color: #1D2541;
				}

				.t2 {
					color: #196AEB;
				}
			}

			.utexts {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #808080;
				line-height: 46rpx;
				margin-top: 10rpx;
			}
		}

	}


	.popup {
		background-color: #FFFFFF;
		height: 900rpx;
		border-radius: 60rpx 60rpx 0rpx 0rpx;
		box-sizing: border-box;
		width: 100%;

		.icon {
			top: 28rpx;
			right: 40rpx;
			position: absolute;
		}

		.tit {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #000000;
			line-height: 38rpx;
			text-align: center;
			padding-top: 44rpx;
		}

		.pflex {
			display: flex;
			flex-wrap: wrap;
			flex-direction: row;
			justify-content: safe;
			padding: 0rpx 30rpx;
			box-sizing: border-box;
			width: 100%;
			overflow-y: auto;
			margin-top: 40rpx;
			height: 760rpx;
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			.itemp {
				width: 220rpx;
				height: 80rpx;
				background: #FFFFFF;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				border: 1rpx solid #D1D1D6;
				text-align: center;
				line-height: 80rpx;
				margin-bottom: 20rpx;
				font-family: DIN, DIN;
				font-weight: bold;
				font-size: 40rpx;
				color: #4F4F4F;
				margin-left: 6rpx;
			}

			.itempactive {
				margin-left: 6rpx;
				width: 220rpx;
				height: 80rpx;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				border: 1rpx solid #D1D1D6;
				text-align: center;
				line-height: 80rpx;
				margin-bottom: 20rpx;
				background: #196AEB;
				font-family: DIN, DIN;
				font-weight: bold;
				font-size: 40rpx;
				color: #ffffff;
			}
		}
	}

	.popup1 {
		background-color: #FFFFFF;
		width: 600rpx;
		border-radius: 30rpx;
		padding: 56rpx 30rpx;
		box-sizing: border-box;
		text-align: center;

		.icon {
			top: 28rpx;
			right: 40rpx;
			position: absolute;
		}

		.img {
			width: 152rpx;
			height: 152rpx;
			margin: 0rpx auto;

			image {
				width: 152rpx;
				height: 152rpx;
			}
		}

		.texts {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #666666;
			margin: 34rpx auto 40rpx;

			span {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
			}
		}

		.context {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #333333;
			line-height: 35rpx;
		}

		.bnts {
			display: flex;
			margin-top: 36rpx;
			justify-content: space-between;

			.btn {
				width: 250rpx;
				height: 80rpx;
				background: #FFFFFF;
				border-radius: 40rpx;
				line-height: 80rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
			}

			.b1 {
				border: 1rpx solid #979797;
				color: #979797;
			}

			.b2 {
				background: #196AEB;
				color: #FFFFFF;
			}
		}
	}

	.yes {
		background-color: #FFFFFF;
		width: 600rpx;
		border-radius: 30rpx;
		padding: 56rpx 30rpx;
		box-sizing: border-box;
		text-align: center;

		.img {
			width: 152rpx;
			height: 152rpx;
			margin: 0rpx auto;

			image {
				width: 152rpx;
				height: 152rpx;
			}
		}

		.ts {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 30rpx;
			color: #333333;
			margin-top: 20rpx;
		}

		.btn {
			height: 80rpx;
			background: #196AEB;
			border-radius: 40rpx;
			margin: 60rpx auto 20rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #FFFFFF;
			line-height: 80rpx;
			text-align: center;
			padding: 0rpx 60rpx;
		}
	}
</style>